package presentation;

import java.awt.Color;
import java.util.Observable;
import java.util.Observer;

import entity.ERegister;

public class PWheelTempMarker implements Observer {
	private double imageWidth;
	private double scaledImageWidth;
	private double wheelX, wheelY;
	private double wheelWidth, wheelHeight;
	private final float alpha = 0.4f;
	private Color leftTempColor = new Color(0f,0f,0f,0);
	private Color centerTempColor = new Color(0f,0f,0f,0);
	private Color rightTempColor = new Color(0f,0f,0f,0);
	public final int LEFT = 0, CENTER = 1, RIGHT = 2;
	private int wheelTempLow, wheelTempMedium, wheelTempHigh;
	
	
	public PWheelTempMarker(double imageWidth, double wheelX, double wheelY, 
			double wheelWidth, double wheelHeight, int wheelTempLow, 
			int wheelTempMedium, int wheelTempHigh) {
		this.imageWidth = imageWidth;
		this.scaledImageWidth = imageWidth;
		this.wheelX = wheelX;
		this.wheelY = wheelY;
		this.wheelWidth = wheelWidth;
		this.wheelHeight = wheelHeight;
		this.wheelTempLow = wheelTempLow;
		this.wheelTempMedium = wheelTempMedium;
		this.wheelTempHigh = wheelTempHigh;
	}
	
	public void setImageWidth(double imageWidth) {
		this.scaledImageWidth = imageWidth;
	}
	
	public int getScaledX() {
		Double scaledX = scaledImageWidth/imageWidth * wheelX;
		return scaledX.intValue();
	}
	
	public int getScaledWidth() {
		Double scaledWidth = scaledImageWidth/imageWidth * wheelWidth;
		return scaledWidth.intValue();
	}
	
	public int getScaledY(int place) {
		Double scaledY = scaledImageWidth/imageWidth * wheelY;
		scaledY += place * (scaledImageWidth/imageWidth * wheelHeight)/3;
		return scaledY.intValue();
	}
	
	public int getScaledHeight() {
		Double scaledHeight = scaledImageWidth/imageWidth * wheelHeight/3;
		return scaledHeight.intValue();
	}
	
	public Color getLeftTempColor() {
		return leftTempColor;
	}
	
	public Color getCenterTempColor() {
		return centerTempColor;
	}
	
	public Color getRightTempColor() {
		return rightTempColor;
	}
	
	public void setTempColor(Color leftTempColor, Color centerTempColor, 
														Color rightTempColor) {
		this.leftTempColor = leftTempColor;
		this.centerTempColor = centerTempColor;
		this.rightTempColor = rightTempColor;
	}

	@Override
	public void update(Observable obs, Object arg) {
		if (obs instanceof ERegister && arg instanceof Number) {
			String register = ((ERegister) obs).getName();
			Color newColor;
			
			if ( (Double) arg < (double)wheelTempLow) {
				newColor = new Color(0f,1f,0f,alpha);
			} else if ( (Double)arg < (double)wheelTempMedium) {
				newColor = new Color(1f,1f,0f,alpha);
			} else if ( (Double)arg < (double)wheelTempHigh) {
				newColor = new Color(1f,0.55f,0f,alpha);
			} else {
				newColor = new Color(1f,0f,0f,alpha);
			}
			
			if (register.endsWith("L")) {
				leftTempColor = newColor;
			} else if (register.endsWith("M")) {
				centerTempColor = newColor;
			} else if (register.endsWith("R")) {
				rightTempColor = newColor;
			}
		}
	}
}
